package com.admin.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.admin.bean.AdminUserBean;
import com.admin.dao.AdminUserDao;

/**   
 * @Title: AdminUserService.java
 * @Package com.admin.service
 * @Description: 
 * @author Jason
 * @date 2010-12-14 上午10:37:33    
 * @version    
 */

@Service
public class AdminUserService {

	private static Logger log = Logger.getLogger(AdminUserService.class);
	
	@Autowired private AdminUserDao adminUserDao;
	
	/**
	  * @Title: addAdminUser
	  * @Description:添加用户 
	  * @param userName
	  * @return int
	  * @throws
	 */
	public boolean addAdminUser(AdminUserBean bean){
		return adminUserDao.addAdminUser(bean)==1?true:false;
	}
	
	/**
	  * @Title: login
	  * @Description: 用户登录
	  * @param userName
	  * @param userPwd
	  * @return boolean
	  * @throws
	 */
	public Map<String,Object> login(String userName,String userPwd){
		AdminUserBean user = adminUserDao.findByUserName(userName);
		Map<String,Object> map = new HashMap<String,Object>();
		if(user==null){
			log.error("用户不存在!");
			map.put("flag", 1);
			return map;
		}
		if(!userPwd.equals(user.getPwd())){
			log.error("用户密码错误!");
			map.put("flag", 3);
			return map;
		}
		map.put("flag", 0);
		map.put("user", user);
		return map;
	}
	
	public AdminUserBean findById(int id){
		return adminUserDao.findById(id);
	}
	
	public int updateAdminUser(AdminUserBean bean){
		return adminUserDao.updateAdminUser(bean);
	}
	
	/**
	  * @Title: findCount
	  * @Description: 查询所有用户数量
	  * @return
	  * @throws
	 */
	public int findCount(){
		return adminUserDao.findCount();
	}
	
	/**
	  * @Title: findAll
	  * @Description: 分页查询所有用户
	  * @param start
	  * @param pageSize
	  * @return
	  * @throws
	 */
	public List<AdminUserBean> findAll(int start,int pageSize){
		return adminUserDao.findAll(start, pageSize);
	}
	
	public List<AdminUserBean> findCp(){
		return adminUserDao.findCp();
	}
	
	public List<AdminUserBean> findCp(String condition){
		return adminUserDao.findCp(condition);
	}
	
	/**
	  * @Title: updateUserPwd
	  * @Description: 修改用户密码
	  * @param userId
	  * @param userPwd
	  * @return
	  * @throws
	 */
	public boolean updateUserPwd(int userId,String userPwd){
		return adminUserDao.updateUserPwd(userId, userPwd)==1?true:false;
	}
	
	/**
	  * @Title: checkUserName
	  * @Description: 判断该用户是否被使用
	  * @param userName
	  * @return true:未被使用 false:被使用
	  * @throws
	 */
	public boolean checkUserName(String userName){
		return adminUserDao.findByUserName(userName)!=null?false:true;
	}
	
	/**
	  * @Title: updateUserStatus
	  * @Description: 修改用户状态
	  * @param userId
	  * @param status
	  * @return
	  * @throws
	 */
	public boolean updateUserStatus(int userId,int status){
		return adminUserDao.updateUserStatus(userId, status)==1?true:false;
	}
}
